package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/21 15:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/cart")
public class CartApiController {


    @Autowired
    private CartService cartService;
    /**
     * 添加购物车控制器。
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum,
                            HttpServletRequest request){
        //  登录userId ，未登录
        String userId = AuthContextHolder.getUserId(request);
        //  判断用户未登录.
        if (StringUtils.isEmpty(userId)){
            //  使用临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  添加购物车：
        cartService.addToCart(skuId,skuNum,userId);
        //  默认返回.
        return Result.ok();
    }

    /**
     * 查看购物车
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        //  获取临时用户Id
        String userTempId = AuthContextHolder.getUserTempId(request);
        //  调用服务层方法
        List<CartInfo> cartInfoList = this.cartService.cartList(userTempId,userId);
        return Result.ok(cartInfoList);
    }

    /**
     * 选中状态变更
     * @param skuId
     * @param isChecked
     * @return
     */
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked,
                            HttpServletRequest request){
        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            //  获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  调用服务层方法.
        this.cartService.checkCart(skuId,isChecked,userId);
        return Result.ok();
    }

    /**
     * 全选
     * @return
     */
    @GetMapping("/allCheckCart/{isChecked}")
    public Result allCheckCart(@PathVariable Integer isChecked,
                               HttpServletRequest request){

        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            //  获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  调用服务层方法.
        this.cartService.allCheckCart(isChecked,userId);
        return Result.ok();
    }

    /**
     * 删除购物车
     * @param skuId
     * @param request
     * @return
     */
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId,
                             HttpServletRequest request){
        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            //  获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  调用服务层方法.
        this.cartService.deleteCart(skuId,userId);
        return Result.ok();
    }

    /**
     * 清空购物车
     * @param request
     * @return
     */
    @GetMapping("clearCart")
    public Result clearCart(HttpServletRequest request){
        //  获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            //  获取临时用户Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //  调用服务层方法.
        this.cartService.clearCart(userId);
        return Result.ok();
    }

    /**
     * 取选中的商品列表
     * @param userId
     * @return
     */
    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId){
        //  获取选中的商品列表
        return this.cartService.getCartCheckedList(userId);
    }
}
